package com.cortex.pineapple;

import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;

import com.cortex.pineapple.utilities.MessagesLog;

public class SpeechQuery extends Service
{
	protected AudioManager mAudioManager; 
	protected SpeechRecognizer speechRecognizer;
	protected Intent mSpeechRecognizerIntent;
	private int returnTo;
	public static final String RESULTS_SPEECH_READY = "com.cortex.pineapple.SpeechQuery.resultsSpeechReady";
	public static final String GET_RESULTS = "getResults";
	public static final String RETURN_TO = "returnTo";


	@Override
	public void onCreate(){
		super.onCreate();
		init();
	}


	private void init(){
		mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
		speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
		speechRecognizer.setRecognitionListener((RecognitionListener) new SpeechRecognitionListener());
		mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
				this.getPackageName());
		speechRecognizer.startListening(mSpeechRecognizerIntent);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		returnTo = intent.getIntExtra(RETURN_TO, 0);
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		if (speechRecognizer != null){
			speechRecognizer.destroy();
		}
	}

	protected class SpeechRecognitionListener implements RecognitionListener{

		@Override
		public void onBeginningOfSpeech(){

		}

		@Override
		public void onBufferReceived(byte[] buffer){

		}

		@Override
		public void onEndOfSpeech(){
			
		}

		@Override
		public void onError(int error){
			Log.d(MessagesLog.APPLICATION,"my service error when SpeechRecognizer error number is" + error);
			SpeechQuery.this.stopSelf();
		}

		@Override
		public void onEvent(int eventType, Bundle params){

		}

		@Override
		public void onPartialResults(Bundle partialResults){

		}

		@Override
		public void onReadyForSpeech(Bundle params){
			mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
			Log.d(MessagesLog.APPLICATION, "onReadyForSpeech"); //$NON-NLS-1$
		}

		@Override
		public void onResults(Bundle results){
			List<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
			Intent i = new Intent(RESULTS_SPEECH_READY);
			i.putExtra(RETURN_TO, returnTo);
			i.putExtra(GET_RESULTS, data.toArray(new String[data.size()]));
			SpeechQuery.this.sendBroadcast(i);
			SpeechQuery.this.stopSelf();
			
		}

		@Override
		public void onRmsChanged(float rmsdB){

		}

	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}